package org.example.demo_ottery.controller;


import org.example.demo_ottery.common.errorcode.ControllerErrorCodeConstants;
import org.example.demo_ottery.common.exception.ControllerException;
import org.example.demo_ottery.common.pojo.CommonResult;
import org.example.demo_ottery.common.utils.JacksonUtil;
import org.example.demo_ottery.controller.param.CreateActivityParam;
import org.example.demo_ottery.controller.param.PageParam;
import org.example.demo_ottery.controller.result.CreateActivityResult;
import org.example.demo_ottery.controller.result.FindActivityListResult;
import org.example.demo_ottery.controller.result.GetActivityDetailResult;
import org.example.demo_ottery.service.ActivityService;
import org.example.demo_ottery.service.dto.ActivityDTO;
import org.example.demo_ottery.service.dto.ActivityDetailDTO;
import org.example.demo_ottery.service.dto.CreateActivityDTO;
import org.example.demo_ottery.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

@RestController
public class ActivityController {
    private final static Logger logger = LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    ActivityService activityService;
    /**
     * 创建活动
     *
     * @param param
     * @return
     */
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(
            @Validated @RequestBody CreateActivityParam param) {
        logger.info("ActivityController createActivity({})"
                , JacksonUtil.writeValueAsString(param));
        return CommonResult.success(
                convertToCreateActivityResult(activityService.createActivity(param)));
    }

    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param) {
        logger.info("ActivityController findActivityList({})",param);
        return CommonResult.success(
                convertToFindActivityResult(activityService.findActivityList(param))
        );
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Long activityId) {
        logger.info("ActivityController getActivityDetail({})",activityId);
        ActivityDetailDTO detailDTO = activityService.getActivityDetail(activityId);
        return CommonResult.success(convertToGetActivityDetailResult(detailDTO));
    }

    private GetActivityDetailResult convertToGetActivityDetailResult(
            ActivityDetailDTO detailDTO) {
        if(null == detailDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult detailResult = new GetActivityDetailResult();
        detailResult.setActivityId(detailDTO.getActivityId());
        detailResult.setDescription(detailDTO.getDesc());
        detailResult.setActivityName(detailDTO.getActivityName());
        detailResult.setValid(detailDTO.valid());
        detailResult.setPrizes(detailDTO.getPrizeDTOList().stream()
                .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTiers().getCode()))
                .map(prizeDTO -> {
                    GetActivityDetailResult.Prize result = new GetActivityDetailResult.Prize();
                    result.setName(prizeDTO.getName());
                    result.setImageUrl(prizeDTO.getImageUrl());
                    result.setPrizeAmount(prizeDTO.getPrizeAmount());
                    result.setPrice(prizeDTO.getPrice());
                    result.setValid(prizeDTO.valid());
                    result.setDescription(prizeDTO.getDescription());
                    result.setPrizeId(prizeDTO.getPrizeId());
                    result.setPrizeTierName(prizeDTO.getTiers().name());
                    return result;
                }).collect(Collectors.toList()));
        detailResult.setUsers(detailDTO.getUserDTOList().stream()
                .map(userDTO -> {
                    GetActivityDetailResult.User result = new GetActivityDetailResult.User();
                    result.setUserName(userDTO.getUserName());
                    result.setUserId(userDTO.getUserId());
                    result.setValid(userDTO.valid());
                    return result;
                }).collect(Collectors.toList()));
        return detailResult;
    }

    private FindActivityListResult convertToFindActivityResult(
            PageListDTO<ActivityDTO> activityList) {
        if(null == activityList) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(activityList.getTotal());
        result.setRecords(activityList.getRecords().stream()
                .map(activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.valid());
                    return activityInfo;
                }).collect(Collectors.toList()));
        return result;
    }

    private CreateActivityResult convertToCreateActivityResult(CreateActivityDTO activity) {
        if(null == activity) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(activity.getActivityId());
        return result;
    }
}
